//
//  MqInfoAuthDetailViewController.m
//  Meiqi
//
//  Created by 王郑 on 16/1/19.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoAuthDetailViewController.h"
#import "MqInfoAuthDetailHeadView.h"
#import "MqInfoListModel.h"
#import "MqInfoListTableViewCell.h"
#import "MqInfoWebViewController.h"
@interface MqInfoAuthDetailViewController () <UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableview;


@property (strong, nonatomic) NSMutableArray *listArray;

@end

@implementation MqInfoAuthDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _listArray = [NSMutableArray array];
    
#pragma maek ----- 自定义导航栏
    [self addNavBar];
    
    [self addTableview];
 
    [self setUpData];
    
    
}


- (void)addNavBar
{
    
    UIView *barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64 * kHeightBase)];
    barView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:barView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10 * kWidthBase, 30 * kHeightBase, 20 * kWidthBase, 20 * kHeightBase);
    [backButton setBackgroundImage:[UIImage imageNamed:@"info-back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [barView addSubview:backButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 300)/2, 20 * kHeightBase, 300*kWidthBase, 40*kHeightBase)];
    titleLabel.text = @"作者介绍";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [barView addSubview:titleLabel];
    
}

- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)setUpData
{
    
    [PKRequestManager requestWithUrlString:_authDetailURL parDic:nil Method:GET finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *list = dic[@"list"];
        for (NSDictionary *oneDic in list) {
            
            MqInfoListModel *model = [[MqInfoListModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
        }
        
        [_tableview reloadData];
        
    } error:^(NSError *error) {
        
//        NSLog(@"%@",error);
    } path:@"AuthDetail"];
    
    
}



- (void)addTableview
{
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 * kHeightBase, kScreenWidth, kScreenHeight - 64*kHeightBase) style:(UITableViewStylePlain)];
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    
    MqInfoAuthDetailHeadView *headView = [[MqInfoAuthDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 250)];
    
    headView.nameLabel.text = _name;
    headView.positionLabel.text =_position;
    [headView.headImageV sd_setImageWithURL:[NSURL URLWithString:_imageUrl] placeholderImage:[UIImage imageNamed:@"info-touxiang"]];
    
    _tableview.tableHeaderView = headView;
    
    [self.view addSubview:_tableview];
    
    
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return 1;
    } else
    
        return _listArray.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *identifier = @"AuthCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            
        }
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
        cell.detailTextLabel.text = _authDescription;
        
        return cell;
        
    } else
    {
        static NSString *identifier = @"AuthListCell";
        MqInfoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            
            cell = [[MqInfoListTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            
        }
        MqInfoListModel *model = _listArray[indexPath.row];
        
        cell.model = model;
        
        return cell;

        
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        return 200 * kHeightBase;
        
    } else
    
    return 80*kHeightBase;
    
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return @"作者介绍";
        
    } else {
        
        return @"专栏作品";
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        
    }
    else {
        
        MqInfoWebViewController *webView = [[MqInfoWebViewController alloc] init];
        
        MqInfoListModel *model = _listArray[indexPath.row];
        NSString *ids = model.topicDetailID;
        NSString *classID = model.classid;
        
        webView.idsURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=1&id=%@&userid=682480&version=1",ids];
        
        // 标签的网址
        webView.tagListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getArticleTagOrTagListByType&type=tagList&id=%@",ids];
        
        // 作者信息的网址
        webView.authorInfoURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getAuthorInfByType&type=authorInfo&classid=%@&id=%@",classID,ids];
        
        // 相关文章的网址
        webView.recommendListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&version=1&platform=1&classid=%@&showType=recommendList",classID];
        // 评论的网址
        webView.commentURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=comment&methodType=getArticleCommentV1&access_token=&classid=%@&id=%@",classID,ids];
        
        [self.navigationController pushViewController:webView animated:YES];

        
    }
    
}

    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
